package com.drakulo.hi.gfx.panel;

import java.util.List;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.dk.hi.core.Settings;
import com.dk.hi.core.Team;
import com.drakulo.hi.gfx.InventoryPanelHeroGfx;
import com.drakulo.hi.gfx.Item;
import com.drakulo.hi.logic.actors.Hero;
import com.drakulo.hi.logic.helpers.InventoryHelper;
import com.drakulo.hi.logic.helpers.MouseHelper;
import com.drakulo.hi.logic.inventory.equipment.Equipment;
import com.drakulo.hi.logic.inventory.equipment.Weapon;
import com.drakulo.hi.states.MenuState;

public class InventoryPanel extends Panel {
	public static final int SUBPANEL_WIDTH = 162;

	private Item draggedItem;
	private int lastDragX;
	private int lastDragY;

	public InventoryPanel() {
		super.initSize(Settings.WINDOW_WIDTH - MenuState.MENU_WIDTH, Settings.WINDOW_HEIGHT);
	}

	private Item getDraggedItem() {
		return this.draggedItem;
	}

	private int getLastDragX() {
		return this.lastDragX;
	}

	private int getLastDragY() {
		return this.lastDragY;
	}

	@Override
	public boolean mouseIsOnIt(int mouseX, int mouseY) {
		return false;
	}

	@Override
	public void render(Graphics g) throws SlickException {
		g.fillRect(MenuState.MENU_WIDTH, 0, Settings.WINDOW_WIDTH - MenuState.MENU_WIDTH, Settings.WINDOW_HEIGHT);

		List<Hero> heroes = Team.getHeroes();
		int currentX = MenuState.MENU_WIDTH;
		currentX += (4 - heroes.size()) * SUBPANEL_WIDTH / 2;

		for (Hero h : heroes) {
			// renderHerosInventory(g, h, currentX);
			InventoryPanelHeroGfx.render(g, h, currentX);
			currentX += SUBPANEL_WIDTH;
		}
		if (getDraggedItem() != null) {
			g.drawImage(getDraggedItem().getSprite(), getLastDragX(), getLastDragY());
		}
	}

	@Override
	public void render(Graphics g, int x, int y) throws SlickException {
		// TODO Auto-generated method stub

	}

	private void setDraggedItem(Item draggedItem) {
		this.draggedItem = draggedItem;
	}

	private void setLastDragX(int lastDragX) {
		this.lastDragX = lastDragX;
	}

	private void setLastDragY(int lastDragY) {
		this.lastDragY = lastDragY;
	}

	@Override
	public void update(Input input) throws SlickException {
		boolean clicked = input.isMousePressed(Input.MOUSE_LEFT_BUTTON);

		// System.out.println(input.getMouseX() + " " + input.getMouseY());
		if (clicked) {
			List<Hero> heroes = Team.getHeroes();
			int currentX = MenuState.MENU_WIDTH;
			currentX += (4 - heroes.size()) * SUBPANEL_WIDTH / 2;

			final int mouseX = input.getMouseX();
			final int mouseY = input.getMouseY();
			if (getDraggedItem() != null) {
				// On est en train de déplacer un objet. Il faut vérifier où on
				// peut le déposer et
				// si on peut le faire
				Item draggedItem = getDraggedItem();
				Hero hoveredHero = null;// Team.getHoveredHero(mouseX, mouseY);

				if (draggedItem instanceof Equipment) {
					// On est en train de déplacer un équipement
					if (draggedItem instanceof Weapon) {
						// On est en train de déplacer une arme
						Item newDraggedItem = null;
						// Le joueur a cliqué...
						if (MouseHelper.mouseIsInInventoryZone(mouseX, mouseY)) {
							// Dans la zone d'inventaire
							newDraggedItem = InventoryHelper.tryItemDrop(hoveredHero, draggedItem, mouseX, mouseY);
							setDraggedItem(newDraggedItem);
						} else if (MouseHelper.mouseIsInEquipmentZone(mouseX, mouseY)) {
							// Dans la zone d'équipement
							Weapon weapon = (Weapon) draggedItem;
							newDraggedItem = InventoryHelper.tryWeaponDrop(hoveredHero, weapon, mouseX, mouseY);
							setDraggedItem(newDraggedItem);
						}
					}
				} else {
					// On est en train de déplacer un objet quelconque
					Item newDraggedItem = InventoryHelper.tryItemDrop(hoveredHero, draggedItem, mouseX, mouseY);
					setDraggedItem(newDraggedItem);
				}
			} else {
				// Le joueur a cliqué...
				if (MouseHelper.mouseIsInInventoryZone(mouseX, mouseY)) {
					// Dans la zone d'inventaire
					Item itemToDrag = InventoryHelper.tryItemDrag(mouseX, mouseY);
					setDraggedItem(itemToDrag);
				} else if (MouseHelper.mouseIsInEquipmentZone(mouseX, mouseY)) {
					// Dans la zone d'équipement
					Item itemToDrag = InventoryHelper.tryEquipmentDrag(mouseX, mouseY);
					setDraggedItem(itemToDrag);
				}

			}

		}

		if (getDraggedItem() != null) {
			// The dragged item is centered on the mouse
			setLastDragX(input.getMouseX() - Item.SPRITE_SIZE / 2);
			setLastDragY(input.getMouseY() - Item.SPRITE_SIZE / 2);

			// The cursor id hided during drag
			MouseHelper.hideCursor();
		} else {
			MouseHelper.showCursor();
		}
	}
}
